// Dart 语言基础示例 - 严格来自 https://dart.cn/language/
import 'dart:math';

void main() {
  print('=== Dart 语言基础示例 ===');
  // 变量声明
  var name = 'Bob';
  Object name2 = 'Bob';
  String name3 = 'Bob';

  // 打印变量值
  print('变量 name 的值: $name');
  print('变量 name2 的值: $name2');
  print('变量 name3 的值: $name3');

  // 可空变量
  String? name4;
  name4 = 'Bob';
  name4 = null;

  // 常量
  const bar = 1000000;
  final baz = [1, 2, 3];

  // 类型推断
  var list = [1, 2, 3];
  var map = {'first': 'Dart', 'second': 'Flutter'};

  // 控制流
  if (name4 == null) {
    print('name4 is null');
  } else {
    print('name4 is not null');
  }

  // 更多示例输出
  print('常量 bar 的值: $bar');
  print('常量 baz 的值: $baz');
  print('列表 list 的和: ${list.reduce((a, b) => a + b)}');
  print('映射 map 的值: ${map.values.join(', ')}');

  // 循环
  for (var i = 0; i < 5; i++) {
    print('hello $i');
  }

  // 函数
  print(greet(name: 'Alice'));

  //注释
  // This is a normal, one-line comment.

  /// This is a documentation comment, used to document libraries,
  /// classes, and their members. Tools like IDEs and dartdoc treat
  /// doc comments specially.

  /* Comments like these are also supported. */

  //导入
  // Importing core libraries
  //import 'dart:math';

  // Importing libraries from external packages
  // import 'package:test/test.dart';

  // Importing files
  //import 'path/to/my_other_file.dart';

  // 类
  var p = Point(2, 3);
  print(p.distanceTo(Point(4, 4)));
}

// 函数定义
String greet({required String name}) {
  return 'Hello, $name!';
}

// 类定义
class Point {
  final double x;
  final double y;

  Point(this.x, this.y);

  double distanceTo(Point other) {
    var dx = x - other.x;
    var dy = y - other.y;
    return sqrt(dx * dx + dy * dy);
  }
}
